/*
 * JuridicalPersonDBTopComponent.java
 * 
 * Created: Jul 6, 2015
 * 
 * Copyright (c) ExpertPB 2015
 * All information contained herein is, and remains the property of
 * ExpertPB and its suppliers, if any.
 */
package com.emex.app.stuff.juridic;

import com.emex.app.shared.data.JuridicalPerson;
import com.emex.app.stuff.gui.model.JuridicalPersontableModel;
import com.emex.app.stuff.provider.JuridicalPersonDataProvider;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import org.netbeans.api.settings.ConvertAsProperties;
import org.openide.awt.ActionID;
import org.openide.awt.ActionReference;
import org.openide.windows.TopComponent;
import org.openide.util.NbBundle.Messages;
import org.openide.windows.WindowManager;

/**
 * Top component which displays something.
 */
@ConvertAsProperties(
        dtd = "-//com.emex.app.stuff.juridic//JuridicalPersonDB//EN",
        autostore = false
)
@TopComponent.Description(
        preferredID = "JuridicalPersonDBTopComponent",
        //iconBase="SET/PATH/TO/ICON/HERE", 
        persistenceType = TopComponent.PERSISTENCE_NEVER
)
@TopComponent.Registration(mode = "editor", openAtStartup = false)
@ActionID(category = "Window", id = "com.emex.app.stuff.juridic.JuridicalPersonDBTopComponent")
@ActionReference(path = "Menu/Юридические лица" /*, position = 333 */)
@TopComponent.OpenActionRegistration(
        displayName = "#CTL_JuridicalPersonDBAction",
        preferredID = "JuridicalPersonDBTopComponent"
)
@Messages({
    "CTL_JuridicalPersonDBAction=Юридические лица",
    "CTL_JuridicalPersonDBTopComponent=Юридические лица",
    "HINT_JuridicalPersonDBTopComponent=Юридические лица"
})
public final class JuridicalPersonDBTopComponent extends TopComponent {

    private JuridicalPersontableModel juridicalPersontableModel;

    public JuridicalPersonDBTopComponent() {
        initComponents();
        setName(Bundle.CTL_JuridicalPersonDBTopComponent());
        setToolTipText(Bundle.HINT_JuridicalPersonDBTopComponent());
        juridicalPersontableModel = new JuridicalPersontableModel();
        jurPersonsTable.setModel(juridicalPersontableModel);
        jurPersonsTable.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
            @Override
            public void valueChanged(ListSelectionEvent e) {
                int row = jurPersonsTable.getSelectedRow();
                if ((row >= 0) && (row < jurPersonsTable.getRowCount())) {
                    editJurPersonButton.setEnabled(true);
                    deleteJurPersonButton.setEnabled(true);
                } else {
                    editJurPersonButton.setEnabled(false);
                    deleteJurPersonButton.setEnabled(false);
                }
            }
        });
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        controlsPanel = new javax.swing.JPanel();
        addJurPersonButton = new javax.swing.JButton();
        editJurPersonButton = new javax.swing.JButton();
        deleteJurPersonButton = new javax.swing.JButton();
        jurPersonsPanel = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jurPersonsTable = new org.jdesktop.swingx.JXTable();

        controlsPanel.setBorder(javax.swing.BorderFactory.createTitledBorder(org.openide.util.NbBundle.getMessage(JuridicalPersonDBTopComponent.class, "JuridicalPersonDBTopComponent.controlsPanel.border.title"))); // NOI18N

        addJurPersonButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/add.png"))); // NOI18N
        org.openide.awt.Mnemonics.setLocalizedText(addJurPersonButton, org.openide.util.NbBundle.getMessage(JuridicalPersonDBTopComponent.class, "JuridicalPersonDBTopComponent.addJurPersonButton.text")); // NOI18N
        addJurPersonButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addJurPersonButtonActionPerformed(evt);
            }
        });

        editJurPersonButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/edit.png"))); // NOI18N
        org.openide.awt.Mnemonics.setLocalizedText(editJurPersonButton, org.openide.util.NbBundle.getMessage(JuridicalPersonDBTopComponent.class, "JuridicalPersonDBTopComponent.editJurPersonButton.text")); // NOI18N
        editJurPersonButton.setEnabled(false);
        editJurPersonButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                editJurPersonButtonActionPerformed(evt);
            }
        });

        deleteJurPersonButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/delete.png"))); // NOI18N
        org.openide.awt.Mnemonics.setLocalizedText(deleteJurPersonButton, org.openide.util.NbBundle.getMessage(JuridicalPersonDBTopComponent.class, "JuridicalPersonDBTopComponent.deleteJurPersonButton.text")); // NOI18N
        deleteJurPersonButton.setEnabled(false);
        deleteJurPersonButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                deleteJurPersonButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout controlsPanelLayout = new javax.swing.GroupLayout(controlsPanel);
        controlsPanel.setLayout(controlsPanelLayout);
        controlsPanelLayout.setHorizontalGroup(
            controlsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(controlsPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(addJurPersonButton)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(editJurPersonButton)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(deleteJurPersonButton)
                .addContainerGap(343, Short.MAX_VALUE))
        );
        controlsPanelLayout.setVerticalGroup(
            controlsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(controlsPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(controlsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(addJurPersonButton)
                    .addComponent(editJurPersonButton)
                    .addComponent(deleteJurPersonButton))
                .addContainerGap())
        );

        jurPersonsPanel.setBorder(javax.swing.BorderFactory.createTitledBorder(org.openide.util.NbBundle.getMessage(JuridicalPersonDBTopComponent.class, "JuridicalPersonDBTopComponent.jurPersonsPanel.border.title"))); // NOI18N

        jurPersonsTable.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jurPersonsTableMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(jurPersonsTable);

        javax.swing.GroupLayout jurPersonsPanelLayout = new javax.swing.GroupLayout(jurPersonsPanel);
        jurPersonsPanel.setLayout(jurPersonsPanelLayout);
        jurPersonsPanelLayout.setHorizontalGroup(
            jurPersonsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jurPersonsPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1)
                .addContainerGap())
        );
        jurPersonsPanelLayout.setVerticalGroup(
            jurPersonsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jurPersonsPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 365, Short.MAX_VALUE)
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jurPersonsPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(controlsPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(controlsPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jurPersonsPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void addJurPersonButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addJurPersonButtonActionPerformed
        JuridicalPersonDialog dialog = new JuridicalPersonDialog(WindowManager.getDefault().getMainWindow(), true);
        dialog.setLocationRelativeTo(null);
        dialog.setTitle("Добавить юридическое лицо");
        dialog.setJuridicalPersontableModel(juridicalPersontableModel);
        dialog.setVisible(true);
    }//GEN-LAST:event_addJurPersonButtonActionPerformed

    private void editJurPersonButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_editJurPersonButtonActionPerformed
        JuridicalPersonDialog dialog = new JuridicalPersonDialog(WindowManager.getDefault().getMainWindow(), true);
        dialog.setLocationRelativeTo(null);
        dialog.setTitle("Редактировать юридическое лицо");
        dialog.setJuridicalPersontableModel(juridicalPersontableModel);
        dialog.setJuridicalPerson(getJuridicalPersonSelection());
        dialog.setVisible(true);
    }//GEN-LAST:event_editJurPersonButtonActionPerformed

    private void jurPersonsTableMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jurPersonsTableMouseClicked
        if (evt.getClickCount() == 2) {
            editJurPersonButtonActionPerformed(null);
        }
    }//GEN-LAST:event_jurPersonsTableMouseClicked

    private void deleteJurPersonButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_deleteJurPersonButtonActionPerformed
        JuridicalPerson selection = getJuridicalPersonSelection();
        if (null != selection) {
            JuridicalPersonDataProvider.getInstance().deleteJuridicalPersonByPrimaryKey(selection.getId());
            juridicalPersontableModel.dataRemoved(selection);
        }
    }//GEN-LAST:event_deleteJurPersonButtonActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton addJurPersonButton;
    private javax.swing.JPanel controlsPanel;
    private javax.swing.JButton deleteJurPersonButton;
    private javax.swing.JButton editJurPersonButton;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JPanel jurPersonsPanel;
    private org.jdesktop.swingx.JXTable jurPersonsTable;
    // End of variables declaration//GEN-END:variables
    @Override
    public void componentOpened() {
        // TODO add custom code on component opening
    }

    @Override
    public void componentClosed() {
        // TODO add custom code on component closing
    }

    void writeProperties(java.util.Properties p) {
        // better to version settings since initial version as advocated at
        // http://wiki.apidesign.org/wiki/PropertyFiles
        p.setProperty("version", "1.0");
        // TODO store your settings
    }

    void readProperties(java.util.Properties p) {
        String version = p.getProperty("version");
        // TODO read your settings according to their version
    }

    protected JuridicalPerson getJuridicalPersonSelection() {
        if (jurPersonsTable.getSelectedRow() > -1) {
            JuridicalPerson pos = (JuridicalPerson) jurPersonsTable
                    .getValueAt(jurPersonsTable.getSelectedRow(), -1);
            return pos;
        }
        return null;
    }
}
